package service

import (
	"context"
	"errors"
	"fmt"
	"shop-srv/basic/config"
	__ "shop-srv/basic/proto"
	"shop-srv/handler/models"
	"strconv"
)

type Server struct {
	__.UnimplementedShopServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Getusername(_ context.Context, in *__.UsernameReq) (*__.UsernameResp, error) {

	return &__.UsernameResp{}, nil
}
func (s *Server) ShopwindowAdd(_ context.Context, in *__.ShopwindowAddReq) (*__.ShopwindowAddResp, error) {
	var sw models.Shopwindow
	sw.Image = in.Image
	sw.UserId = int(in.UserId)
	sw.Name = in.Name
	sw.Stock = int(in.Stock)
	sw.Status = int(in.Status)

	sw.Price = float64(in.Price)
	sw.ShopwindowCargetryId = int(in.ShopwindowCargetryId)
	//if sw.ID == 0 {
	//	return nil, errors.New("商品不存在")
	//}
	if err := sw.CreateShop(config.DB); err != nil {
		return nil, errors.New("商品橱窗添加失败")
	}
	return &__.ShopwindowAddResp{
		Id: int64(sw.ID),
	}, nil
}

func (s *Server) ShopwindowCargetryAdd(_ context.Context, in *__.ShopwindowCargetryAddReq) (*__.ShopwindowCargetryAddResp, error) {
	var c models.ShopwindowCargetry
	c.ShopwindowId = int(in.ShopwindowId)
	c.Caregoryname = in.Caregoryname
	//if err := c.Findshop(config.DB, in.Caregoryname); err != nil {
	//	return nil, errors.New("橱窗分类名字重复不能添加")
	//}

	tx := config.DB.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	if err := tx.Error; err != nil {
		return nil, err
	}

	if err := tx.Create(&models.ShopwindowCargetry{

		Caregoryname: c.Caregoryname,
		ShopwindowId: c.ShopwindowId,
	}).Error; err != nil {
		tx.Rollback()
	}

	if err := c.CreateShop(config.DB); err != nil {
		return nil, errors.New("商品橱窗分类添加成功失败")
	}
	return &__.ShopwindowCargetryAddResp{
		Id: int64(c.ID),
	}, nil
}

func (s *Server) ShopAdd(_ context.Context, in *__.ShopAddReq) (*__.ShopAddResp, error) {
	var sw models.Shop
	sw.Image = in.Image
	sw.UserId = int(in.UserId)
	sw.Name = in.Name
	sw.Num = strconv.FormatInt(in.Num, 10)
	sw.Status = int(in.Status)

	sw.Price = float64(in.Price)
	sw.ShopwindowCargetryId = int(in.ShopwindowCargetryId)

	if err := sw.CreateShop(config.DB); err != nil {
		return nil, errors.New("商品橱窗添加失败")
	}
	return &__.ShopAddResp{
		Id: int64(sw.ID),
	}, nil
}

func (s *Server) ShopwindowUpdate(_ context.Context, in *__.ShopwindowUpdateReq) (*__.ShopwindowUpdateResp, error) {
	var sw models.Shopwindow
	sw.ID = uint(in.Id)
	sw.Status = int(in.Status)
	if err := sw.Findshop(config.DB, int(in.Id)); err != nil {
		return nil, fmt.Errorf("查询失败了")
	}
	if sw.Status == 1 {
		if err := sw.Update(config.DB, int(in.Id), int(in.Status)); err != nil {
			return nil, errors.New("商品状态同步失败")
		}
	}
	return &__.ShopwindowUpdateResp{
		Id: int64(sw.ID),
	}, nil
}

func (s *Server) OrderXinxi(_ context.Context, in *__.OrderXinxReq) (*__.OrderXinxResp, error) {
	var sw models.Order
	sw.OrderId = int(in.OrderId)
	sw.Status = int(in.Status)
	sw.UserId = int(in.UserId)
	sw.ShopId = int(in.ShopId)
	if sw.Status == 1 {
		return nil, fmt.Errorf("订单失效了")
	}
	if err := sw.Createorder(config.DB); err != nil {
		return nil, err
	}
	return &__.OrderXinxResp{
		Id: int64(sw.ID),
	}, nil
}
